package com.maserb.monitor.remote;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.card.EcCardEntity;
import com.maserb.monitor.entity.card.RechargeRecordEntity;
import com.maserb.monitor.entity.schoolNotice.SchoolNoticeEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentEntity;
import com.maserb.monitor.model.TransactionModel;
import com.maserb.monitor.repository.schoolStudent.RechargeRecordRepository;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.remote.EcCardService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 卡德智能一卡通接口
 */
@Controller
@RequestMapping("/ecCard")
public class EcCardController {
    @Autowired
    EcCardService ecCardService;
    @Autowired
    SchoolStudentService schoolStudentService;
    @Autowired
    RechargeRecordRepository rechargeRecordRepository;
    @Autowired
    UserService userService;

    @RequestMapping("/Code")
    @ResponseBody
    public AjaxResult<Boolean> testQrCode() {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                List<SchoolStudentEntity> studentList = schoolStudentService.findByClazzName("2021%");
                for(SchoolStudentEntity student : studentList) {
                    QrCodeUtil.generateQRCode(student.getId(),200,200,"png","c:\\qrcode\\" + student.getName() + "-" + student.getOrgName() + ".png");
                }

                return true;
            }
        });
    }

    @RequestMapping("/addRecharge")
    @ResponseBody
    public AjaxResult<Boolean> addRecharge() {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                RechargeRecordEntity recharge = rechargeRecordRepository.findById("2c90ee426527cc66016527d05b5a0001").get();
                ecCardService.doAddRechargeRecord(recharge);
                return true;
            }
        });
    }
    /**
     * 充值记录
     * @param page
     * @param studentId
     * @return
     */
    @RequestMapping("/rechargeRecordList")
    @ResponseBody
    public PageResponseResult<RechargeRecordEntity> rechargeRecordList(PageRequestParam page, String studentId, String type) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<RechargeRecordEntity>>() {
            @Override
            public Page<RechargeRecordEntity> invoke(PageRequest var1) throws Exception {
                Page<RechargeRecordEntity> page = ecCardService.findAllRechargeRecord(var1, studentId, type);
                for(RechargeRecordEntity entity : page.getContent()){
                    if(null==entity.getRechargeStatus()||StringUtils.isEmptyOrWhitespace(entity.getRechargeStatus())){
                        entity.setRechargeStatus("0");
                    }
                }
                return page;
            }
        });
    }

    /**
     * 查询交易记录
     * @param page
     * @param studentId
     * @param type 1：学生 2：老师
     * @return
     */
    @RequestMapping("/transactionList")
    @ResponseBody
    public AjaxResult<List<TransactionModel>> transactionList(PageRequestParam page, String studentId, String type) {
        return AjaxUtils.process(new Func_T<List<TransactionModel>>() {
            @Override
            public List<TransactionModel> invoke() throws Exception {
                List<TransactionModel> transactionList = ecCardService.queryTransaction(page, studentId, type);
                return transactionList;
            }
        });
    }

    /**
     * 余额查询
     * @param studentId
     * @param type 1：学生 2：老师
     * @return
     */
    @RequestMapping("/studentBalance")
    @ResponseBody
    public AjaxResult<String> studentBalance(String studentId, String type) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                if(StringUtils.isEmpty(type) || type.equals("1")) {
                    SchoolStudentEntity student = schoolStudentService.findOne(studentId);
                    if(student.getCid() != null) {
                        return ecCardService.queryBalance(student.getCid().toString());
                    }
                } else {
                    UserEntity teacher = userService.findOne(studentId);
                    if(teacher.getCid() != null) {
                        return ecCardService.queryBalance(teacher.getCid().toString());
                    }
                }

                return "0";
            }
        });
    }


    /**
     * 接受卡德发卡数据
     * @param card
     * @return
     */
    @RequestMapping("/doAddEcCard")
    @ResponseBody
    public AjaxResult<Boolean> doAddEcCard(EcCardEntity card) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                ecCardService.saveCard(card);
                return true;
            }
        });
    }

    /**
     * 接受卡德充值领用结果
     * @param record
     * @return
     */
    @RequestMapping("/doUpdateRechargeRecord")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateRechargeRecord(RechargeRecordEntity record) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                if(!StringUtils.isEmpty(record.getId())) {
                    RechargeRecordEntity dbRecord = ecCardService.findOne(record.getId());
                    //0 领用失败  1  领用成功
                    dbRecord.setRechargeStatus(record.getRechargeStatus());
                    ecCardService.save(dbRecord);
                }

                return true;
            }
        });
    }
}
